<?php
class Transaction_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function get_updated($day){
		if(is_int($day)){
			$this->db->where('transaction_modified_date >= DATE_SUB(NOW(), INTERVAL '.$day.' DAY)');
			$query = $this->db->get('transaction');
			return $query->result();
		}else
			return null;
	}

	public function add($branch_id, $file_name, $profit){
		$data = array(
				'branch_id' => $branch_id,
				'transaction_file_location' => $file_name,
				'transaction_profit' => $profit
		);
		if ($this->db->insert('transaction', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function get_transaction_by_branch_id($id){
		$this->db->where('branch_id', $id);
		$this->db->order_by("transaction_modified_date", "desc");
		$query = $this->db->get('transaction');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}else
			return null;
	}

	public function get_transaction_detail_by_submit_id($submit_id){
		$this->db->select('*');
		$this->db->from('transaction_detail');
		$this->db->join('item', 'transaction_detail.item_barcode = item.item_barcode');
		$this->db->where('submit_id', $submit_id);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}else
			return null;
	}

	public function get_transaction($week){

		$this->db->select('*');
		$this->db->from('transaction');
		$this->db->join('branch', 'transaction.branch_id = branch.branch_id', 'right outer');
		$this->db->where('month(transaction_modified_date) >= (month(now()) - '.$week.')');
		$this->db->or_where('transaction_created_date is null');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return null;
	}

	public function get_transaction_by_id($id){
		$query = $this->db->where('transaction_id', $id)->get('transaction');

		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}


	public function add_detail($submit_id, $transaction_id,$branch_id, $cashier_id, $item_barcode, $transaction_quantity, $total_price, $transaction_date){
		$this->load->model('item_model');
		$item_id = $this->item_model->get_item_by_barcode($item_barcode)->item_id;
		if ($item_id){
			$date = DateTime::createFromFormat('d/m/Y H:i:s', $transaction_date);
			$newformat = $date->format('Y-m-d H:i:s');
			$data = array(
					'submit_id' => $submit_id,
					'transaction_id' => $transaction_id,
					'branch_id' => $branch_id,
					'cashier_id' => $cashier_id,
					'item_id' => $item_id,
					'transaction_quantity' => $transaction_quantity,
					'total_price' => $total_price,
					'transaction_date' => $newformat
			);
			if ($this->db->insert('transaction_detail', $data))
				return $this->db->insert_id();
			else
				return -1;
		}else
			return -1;
	}

	public function search($submit_id, $string, $limit, $start){
		$this->db->select('*');
		$this->db->like('transaction_id', $string);
		$this->db->limit($limit, $start);
		$this->db->where('submit_id', $submit_id);
		$this->db->from('transaction_detail');
		$this->db->join('item', 'transaction_detail.item_barcode = item.item_barcode');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return null;
	}

	public function count_search($submit_id, $string){
		$this->db->like('transaction_id', $string);
		$this->db->where('submit_id', $submit_id);
		$this->db->from('transaction_detail');
		return $this->db->count_all_results();
	}

	public function search_barcode($search_type, $string, $limit, $start){

		if ($search_type == 0){
			$this->db->select('*');
			$this->db->like('transaction_id', $string);
			$this->db->limit($limit, $start);
			$this->db->from('transaction_detail');
			$this->db->join('item', 'transaction_detail.item_barcode = item.item_barcode');
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				return $query->result();
			}
			return null;
		}else{
				
			$this->db->select('sum(total_price - (transaction_quantity * item_cost)) as "sum", item.item_name, item.item_barcode, sum(transaction_quantity) as "quantity"');
			$this->db->from('transaction_detail');
			$this->db->join('item', 'item.item_barcode = transaction_detail.item_barcode');
			$this->db->like('item.item_barcode', $string);
			$this->db->limit($limit, $start);
			$this->db->group_by('transaction_detail.item_barcode');
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				return $query->result();
			}
			return null;
		}
	}

	public function gettopsale($limit){

		$this->db->select('sum(total_price - (transaction_quantity * item_cost)) as "sum", item.item_name, item.item_barcode, sum(transaction_quantity) as "quantity"');
		$this->db->from('transaction_detail');
		$this->db->join('item', 'item.item_barcode = transaction_detail.item_barcode');
		$this->db->limit($limit);
		$this->db->group_by('transaction_detail.item_barcode');
		$this->db->order_by("sum", "desc");
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return null;

	}

	public function count_search_barcode($search_type, $string){
		if ($search_type == 0){
			$this->db->like('transaction_id', $string);
		}else{
			$this->db->like('item.item_barcode', $string);
		}
		$this->db->from('transaction_detail');
		$this->db->join('item', 'transaction_detail.item_barcode = item.item_barcode');
		return $this->db->count_all_results();
	}

}